package com.sky.controller.user;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@RestController("userDishController")
@RequestMapping("/user/dish")
@Api(tags = "C端菜品浏览接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    /*使用传统redis*/
   /* public Result<List<DishVO>> list(Long categoryId) {
        String key = "dish_" + categoryId;
        //在redis中存有
        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
        if (!CollectionUtils.isEmpty( list)) {
            return Result.success(list);
        }
        //不在redis中
        list = dishService.listWithFlavors(categoryId);
        redisTemplate.opsForValue().set(key, list);
        return Result.success(list);
    }*/

    /*使用spring cache*/
    @Cacheable(cacheNames = "dishCache", key = "#categoryId")
    public Result<List<DishVO>> list(Long categoryId) {
        List<DishVO> list = dishService.listWithFlavors(categoryId);
        log.info("查询结果：{}", list);
        return Result.success(list);
    }
}
